package com.aidex.web.controller.biz;

import com.aidex.biz.service.WeChatLoginService;
import com.aidex.biz.service.WxPayService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.domain.entity.Company;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.system.service.CompanyService;
import com.aidex.system.service.ISysUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 公司信息Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-03-04
 */
@RestController
@RequestMapping("/biz/bizCompany")
public class CompanyController extends BaseController {
    @Autowired
    private CompanyService companyService;
    @Autowired
    private WxPayService wxPayService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private WeChatLoginService weChatLoginService;

    /**
     * 查询公司信息列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(Company company, HttpServletRequest request, HttpServletResponse response) {

        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            company.setId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        company.setPage(new PageDomain(request, response));
        return R.data(companyService.findPage(company));
    }

    /**
     * 获取公司信息详细信息
     */
    @GetMapping(value = "/{id}")
    public R<Company> detail(@PathVariable("id") String id) {
        return R.data(companyService.get(id));
    }

    /**
     * 新增公司信息
     */
    @Log(title = "公司信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated Company company) {
        //设置公司信息
        String id = StringUtils.Getnum();
        id = id.substring(6);
        company.setId(id);
        company.setIsNewRecord();
        if (companyService.save(company)) {
            // 开通公司管理员账号
//            SysUser user = new SysUser();
//            user.setCompanyId(id);
//            user.setUserName(company.getLianxiPhone());
//            user.setName(company.getLianxiUser());
//            user.setNickName(company.getLianxiUser());
//            user.setPassword("123456");
//            user.setRoleId("2");
//            user.setPosts("2");
//            user.setDeptId("39a06d06cb694cb0ae9e72aec14474ab");
//            user.setId(UUID.randomUUID().toString());
//            iSysUserService.insertUser(user);
            return R.status(true);
        } else {
            return R.status(false);
        }


    }

    /**
     * 修改公司信息
     */
    @Log(title = "公司信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated Company company) {
        if (StringUtils.isEmpty(company.getId())) {
            String id = StringUtils.Getnum();
            id = id.substring(9);
            company.setId(id);
            company.setIsNewRecord();
            if (companyService.save(company)) {
                // 开通公司管理员账号
//                SysUser user = new SysUser();
//                user.setCompanyId(id);
//                user.setUserName(id);
//                user.setName(company.getCompanyName());
//                user.setPassword("123456");
//                user.setRoleIds(new String[]{"2"});
//                user.setPostIds(new String[]{"1"});
//                user.setUserType("1");
//                user.setStatus("0");
//                user.setDelFlag("0");
//                user.setNo(user.getUserName());
//                user.setPhonenumber(company.getLianxiPhone());
//                iSysUserService.insertUser(user);


                return R.status(true);
            } else {
                return R.status(false);
            }
        } else {
            return R.status(companyService.save(company));
        }

    }


    /**
     * 删除公司信息
     */
    @Log(title = "公司信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(companyService.deleteCompanyByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(companyService.findMaxSort(new Company()));
    }

    /**
     * 导出公司信息列表
     */
    @Log(title = "公司信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(Company company) {
        List<Company> list = companyService.findList(company);
        ExcelUtil<Company> util = new ExcelUtil<Company>(Company.class);
        return util.exportExcel(list, "公司信息数据");
    }

    /**
     * 获取当前登录用户的companyID
     */
    @GetMapping("/getCompanyId")
    public R getCompanyId() {
        return R.data(companyService.get(SecurityUtils.getLoginUser().getUser().getCompanyId()));
    }

    /**
     * 获取当前登录用户的companyID
     */
    @GetMapping("/getXcxUrl")
    public R getXcxUrl(String id) {
        Company company = companyService.get(id);
        weChatLoginService.setAppId(company.getWechatXappid(), company.getWechatXappSecret());
        String xcxUrl = weChatLoginService.getXcxUrl();
        company.setXcxurl(xcxUrl);
        companyService.save(company);
        return R.data(company);
    }

    /**
     * 审核公司信息
     */
    @GetMapping("/passCompany")
    public R passCompany(String id) {
        Company company = companyService.get(id);
        company.setStatus("1");
//        if(companyService.save(company)){
//            // 开通公司管理员账号
//            SysUser user = new SysUser();
//            user.setCompanyId(id);
//            user.setUserName(company.getLianxiPhone());
//            user.setName(company.getLianxiUser());
//            user.setNickName(company.getLianxiUser());
//            user.setPassword("123456");
//            user.setRoleId("2");
//            user.setPosts("2");
//            user.setDeptId("39a06d06cb694cb0ae9e72aec14474ab");
//            user.setId(UUID.randomUUID().toString());
//            iSysUserService.insertUser(user);
//        }
        return R.status(companyService.save(company));
    }




}
